package cn.sdormitory.job.dormitory;

import cn.sdormitory.attendance.dao.HolidayConfigDao;
import cn.sdormitory.attendance.entity.HolidayConfig;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.sms.TxSmsTemplate;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.smartdor.dao.SdWeekendAttenceDao;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import org.apache.http.client.utils.DateUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: WeekendNotClockRecordsNoticeJob
 * @author: Alex
 * @date: 2024/9/21
 **/
public class WeekendNotClockRecordsNoticeJob extends QuartzJobBean {
	
	@Autowired
	SysUserDao sysUserDao;
	
	@Autowired
	SdWeekendAttenceDao sdWeekendAttenceDao;
	
	@Autowired
	private TxSmsTemplate txSmsTemplate;
	
	@Autowired
	private HolidayConfigDao holidayConfigDao;
	
	@Autowired
	private SysDeptDao sysDeptDao;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		Date date = new Date();
		String accessDate = DateUtils.formatDate(date, "yyyy-MM-dd");

//		String accessDate = "2024-09-12";
		//获取所有假期
		List<HolidayConfig> holiday = holidayConfigDao.getHolidayByDate(accessDate);
		
		// 如果没有添加假期数据
		if (holiday.isEmpty()) {
			return;
		}
		
		// 如果有假期数据，则根据部门假期配置选取节假日最后一天的考勤数据
		boolean qmxNeedClockIn = false;
		boolean bcspNeedClockIn = false;
		boolean sxNeedClockIn = false;
		// 遍历假期数据，如果部门放假的最后一天是当天，则查看该部门下的学生是否打卡，如果不是当天，则不需要查看该学生的打卡信息。
		for (HolidayConfig holidayConfig : holiday) {
			if (holidayConfig.getDeptId() == 12  && holidayConfig.getHolidayEnd().equals(accessDate)) {
				qmxNeedClockIn = true;
			}
			
			if (holidayConfig.getDeptId() == 6 && holidayConfig.getHolidayEnd().equals(accessDate)) {
				bcspNeedClockIn = true;
			}
			
			if (holidayConfig.getDeptId() == 7 && holidayConfig.getHolidayEnd().equals(accessDate)) {
				sxNeedClockIn = true;
			}
		}
		
		// 如果都不需要打卡，直接返回。
		if (!sxNeedClockIn && !bcspNeedClockIn && !qmxNeedClockIn) {
			return;
		}
		
		//查询所有班主任老师
		List<SysUser> classTeacher = sysUserDao.getUserListByRole("classTeacher");
		
		Map<String, String[]> modelMsg = new HashMap<>();
		for(SysUser user : classTeacher){
			//判断当前班主任老师所在部门是否放假
			boolean flag = false;
			if(holiday != null && holiday.size() > 0){
				for(HolidayConfig item : holiday){
					//判断当前放假部门是否有子部门
					List<Long> deptChildId = sysDeptDao.getAllChildByParent(item.getDeptId());
					flag = deptChildId.stream().anyMatch(temp -> temp.equals(user.getDeptId()));
					if(flag){
						break;
					}
				}
			}
			//放假
			if(flag){
				continue;
			}
			
			//获取宿舍未归寝学生名单
			List<BStudent> studentList = sdWeekendAttenceDao.getClassUnderJurisdictionOfStuByStatus(user.getId(), accessDate, "2");
			if(studentList.size() <=0 || studentList == null){
				continue;
			}
			
			//拼凑短信内容
			StringBuilder smsContent = new StringBuilder();
			if (studentList.size() > 15) {
				for (int i = 0; i < studentList.size() / 15; i ++) {
					List<BStudent> bStudents = studentList.subList(i * 15, 15 * (i + 1) - 1 );
					for (BStudent bStudent : bStudents) {
						smsContent.append(bStudent.getStudentName()).append("，");
					}
					String[] params = {accessDate,smsContent.substring(0, smsContent.length() - 1) + "。"};
					modelMsg.put(user.getPhone(), params);
				}
			} else {
				for (BStudent student : studentList) {
					smsContent.append(student.getStudentName()).append("，");
				}
				
				String[] params = {accessDate, smsContent.substring(0, smsContent.length() - 1) + "。"};
				modelMsg.put(user.getPhone(), params);
			}
//			if(user.getPhone() != null){
//				txSmsTemplate.sendMesModel(user.getPhone(), params, 5);
////                txSmsTemplate.sendMesModel("18152606788", params, 5);
//			}
		}
		
		for (Map.Entry<String, String[]> stringEntry : modelMsg.entrySet()) {
			txSmsTemplate.sendMesModel(stringEntry.getKey(), stringEntry.getValue(), 5);
		}
	}
}
